//
//  XOWMRegisterViewController.m
//  XOWMSDK
//
//  Created by skyline on 2018/7/26.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "XOWMRegisterViewController.h"
#import "XOWMBindViewController.h"
#import "XOWMRegisterView.h"
#import "XOWMPDF.h"
#import "XOWMRequest.h"
@interface XOWMRegisterViewController ()<YJDRegisterViewDelegate,YJDRequestDelegate>
@property (nonatomic, strong) XOWMRegisterView *registerView;
@property (nonatomic, strong) XOWMPDF*protocolView;
@property (nonatomic, strong) XOWMRequest *request;
@property (nonatomic, assign) YJDRegisterViewEvent event;
@property (nonatomic, strong) NSDictionary *accountDict;
@property (nonatomic, copy) NSString *code;
@end

@implementation XOWMRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (XOWMRegisterView *)registerView
{
    if (!_registerView)
    {
        _registerView = [[XOWMRegisterView alloc] init];
        _registerView.delegate = self;
        [self.view addSubview:_registerView];
        _registerView.c_center.equal(self.view);
        _registerView.c_size.equal(self.registerView.bounds.size);
    }
    return _registerView;
}

- (XOWMPDF *)protocolView
{
    if (!_protocolView)
    {
        _protocolView = [[XOWMPDF alloc] initWithPDF:@"protocol"];
        [self.view addSubview:_protocolView];
        _protocolView.c_edges.equal(UIEdgeInsetsZero);
    }
    return _protocolView;
}

- (XOWMRequest *)request
{
    if (!_request)
    {
        _request = [[XOWMRequest alloc]init];
        _request.delegate = self;
    }
    return _request;
}

#pragma mark - YJDRegisterViewDelegate
- (void)yjd_YJDRegisterViewWithEvent:(YJDRegisterViewEvent)event dict:(NSDictionary *)dict
{
    self.event = event;
    if (event == YJDRegisterViewEventBack)
    {
        return [self dismissViewControllerAnimated:YES completion:nil];
    }
    else if (event == YJDRegisterViewEventPDF)
    {
        self.protocolView.hidden = NO;
        return;
    }
    else if (event == YJDRegisterViewEventNameRegister)
    {
        if ([self.registerView.nameRegisterView.nameTF.text length] < 1) return [CCAlert showMessage:@"请输入6-10位账号" hidden:nil];
        else if ([self.registerView.nameRegisterView.passTF.text length] < 1) return [CCAlert showMessage:@"密码不能为空" hidden:nil];
        else if (![self.registerView.nameRegisterView.nameTF.text yjd_isNumberOralphabet]) return [CCAlert showMessage:@"账号仅限于数字或大小写字母" hidden:nil];
        else if (![self.registerView.nameRegisterView.passTF.text yjd_isNumberOralphabet]) return [CCAlert showMessage:@"密码只能包含数字和字母" hidden:nil];
    }
    else if(event == YJDRegisterViewEventPhoneRegister)
    {
        if ([self.registerView.phoneRegisterView.phoneTF.text length] < 1) return [CCAlert showMessage:@"手机号码不能为空" hidden:nil];
        else if ([self.registerView.phoneRegisterView.passTF.text length] < 1) return [CCAlert showMessage:@"密码不能为空" hidden:nil];
        else if ([self.registerView.phoneRegisterView.verifyTF.text length] < 1) return [CCAlert showMessage:@"验证码不能为空" hidden:nil];
        else if (![self.registerView.phoneRegisterView.phoneTF.text yjd_isMobile]) return [CCAlert showMessage:@"请输入正确的手机号码" hidden:nil];
        else if (![self.registerView.phoneRegisterView.passTF.text yjd_isNumberOralphabet]) return [CCAlert showMessage:@"密码只能包含数字和字母" hidden:nil];
        else if (![self.registerView.phoneRegisterView.verifyTF.text isEqualToString:self.code]) return [CCAlert showMessage:@"验证码错误" hidden:nil];
    }
    else if (event == YJDRegisterViewEventVerfiy)
    {
        if ([self.registerView.phoneRegisterView.phoneTF.text length] < 1) return [CCAlert showMessage:@"手机号码不能为空" hidden:nil];
        else if (![self.registerView.phoneRegisterView.phoneTF.text yjd_isMobile]) return [CCAlert showMessage:@"手机号码有误" hidden:nil];
        else
        {
            [self.request yjd_verifyCodeRequest:dict];
        }
        return;
    }
    
    self.request = nil;
    self.accountDict = dict;
    [CCHud startHud];
    [self.request yjd_registerRequst:dict];
}

#pragma mark - YJDRequestDelegate
- (void)yjd_didRequestWithReceiveData:(NSDictionary *)dict
{
    //关闭菊花
    [CCHud stopHud];
    
    if (!dict)
        return;
    
    int code = [[dict valueForKey:@"code"] intValue];
    NSString *info = [dict valueForKey:@"info"];
    //成功
    if (code == 100)
    {
        if (self.event == YJDRegisterViewEventVerfiy)
        {
            self.code = info;
            return;
        }
        helperManager().insertAccount(self.accountDict);
        [CCAlert showMessage:info hidden:^{
            //1.如果是用户名注册，成功后，跳转到绑定手机界面
            if (self.event == YJDRegisterViewEventNameRegister && VERSION_2_0_0_LATER)
            {
                XOWMBindViewController *bind = [[XOWMBindViewController alloc] init];
                bind.accountid = [dict valueForKey:@"accountid"];
                [self presentViewController:bind animated:YES completion:nil];
                return;
            }
            //2.如果不是登录界面进入的注册，且是手机号码注册，成功后，跳转到登录界面
            else if ([self.presentingViewController isKindOfClass:NSClassFromString(@"ViewController")])
            {
                [self dismissViewControllerAnimated:NO completion:^{
                    platformManager().prepareLogin();
                }];
                return;
            }
            //3.如果是从登录的注册界面，且是手机号码注册，成功后，跳转到登录界面
            else if ([self.presentingViewController isKindOfClass:NSClassFromString(@"XOWMLoginViewController")])
            {
                [self dismissViewControllerAnimated:YES completion:nil];
                return;
            }
            
        }];
    }
    //失败
    else
    {
        [CCAlert showMessage:[NSString stringWithFormat:@"%@" ,info] title:@"错误" enter:nil];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.registerView.nameRegisterBtn.selected = YES;
    self.registerView.phoneRegisterBtn.selected = NO;
    [self.registerView.phoneRegisterBtn setValue:[UIColor grayColor] forKeyPath:@"_titleLBL.textColor"];
    self.registerView.nameRegisterBtn.content = (helperManager().getPara().style == XOWMPlatformStyleImage)?@"yjd_注册_账号_title":@"账号注册";
    if (VERSION_1_2_0_LATER)
        self.registerView.phoneRegisterBtn.content = (helperManager().getPara().style == XOWMPlatformStyleImage)?@"yjd_注册_手机_title":@"手机注册";
    self.registerView.nameRegisterView.registerBtn.content = (helperManager().getPara().style == XOWMPlatformStyleImage)?@"yjd_注册_Btn":@"注册";
    self.registerView.phoneRegisterView.registerBtn.content = (helperManager().getPara().style == XOWMPlatformStyleImage)?@"yjd_注册_Btn":@"注册";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


- (void)sp_upload {
    NSLog(@"Get User Succrss");
}
@end
